package com.banciyuan.bcywebview.biz.circles.discover.tagnav;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.biz.circles.CircleLikeHelper;
import com.banciyuan.bcywebview.biz.circles.smooth.CircleTagSmoothActivity;
import com.banciyuan.bcywebview.utils.common.UIUtils;
import com.banciyuan.bcywebview.utils.intent.gotoUtil;
import com.banciyuan.bcywebview.utils.universalimageloader.core.ImageLoader;
import com.banciyuan.bcywebview.utils.universalimageloader.utils.ImageOptions;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.daoexample.model.FollowTag;

/**
 * Created by squall on 2017/3/20.
 */

public class DiscoverTagNavRightAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;
    private List<FollowTag> tlist = new ArrayList<>();
    private String name;

    public DiscoverTagNavRightAdapter(Context mContext, List<FollowTag> tlist) {
        this.mContext = mContext;
        this.tlist = tlist;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return 0;
        } else if (position == tlist.size() + 1) {
            return 2;
        } else {
            if (!TextUtils.isEmpty(tlist.get(position - 1).getType_name())) {
                return -1;
            } else {
                return 1;
            }
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == 0) {
            return new TitleHolder(View.inflate(mContext, R.layout.discover_tag_right_title, null));
        } else if (viewType == 1) {
            return new ItemHolder(View.inflate(mContext, R.layout.discover_tag_right_item, null));
        } else if (viewType == -1) {
            return new TitleHolder(View.inflate(mContext, R.layout.discover_tag_right_title, null));
        } else {
            return new BlankHolder(View.inflate(mContext, R.layout.smooth_blank, null));
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        if (getItemViewType(position) == 1) {
            ItemHolder itemHolder = (ItemHolder) holder;
            final FollowTag tagDetail = tlist.get(position - 1);
            ImageLoader.getInstance().displayImage(tagDetail.getCover(), itemHolder.img, ImageOptions.options);
            if (!TextUtils.isEmpty(tagDetail.getTag_name())) {
                itemHolder.tag_name.setText(tagDetail.getTag_name());
            }
            if (tagDetail.isTf_status()) {
                itemHolder.focus.setSelected(true);
            } else {
                itemHolder.focus.setSelected(false);
            }

            Cilck cilck = new Cilck(position - 1);
            itemHolder.focus.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(final View v) {
                    if (!v.isSelected()) {
                        CircleLikeHelper.doCircleLike(mContext, tagDetail.getTag_id(), "tag", "follow", new CircleLikeHelper.CircleLikeSuccess() {
                            @Override
                            public void onCircleLikeSuccess() {
                                tagDetail.setTf_status(true);
                                v.setSelected(!v.isSelected());
                            }
                        });
                    }
                }
            });
            itemHolder.tag_name.setOnClickListener(cilck);
            itemHolder.img.setOnClickListener(cilck);
        } else if (getItemViewType(position) == 0) {
            TitleHolder titleHolder = (TitleHolder) holder;
            if (!TextUtils.isEmpty(name)) {
                titleHolder.title.setVisibility(View.VISIBLE);
                titleHolder.title.setText(String.format(mContext.getString(R.string.tag_circle_unit), name));
            } else {
                titleHolder.title.setVisibility(View.GONE);
            }
        } else if (getItemViewType(position) == -1) {
            final FollowTag tagDetail = tlist.get(position - 1);
            TitleHolder titleHolder = (TitleHolder) holder;
            if (!TextUtils.isEmpty(tagDetail.getType_name())) {
                titleHolder.title.setVisibility(View.VISIBLE);
                titleHolder.title.setText(tagDetail.getType_name());
            } else {
                titleHolder.title.setVisibility(View.GONE);
            }
        } else {
            BlankHolder blankHolder = (BlankHolder) holder;
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, UIUtils.dip2px(16, mContext));
            blankHolder.blank.setLayoutParams(layoutParams);
        }

    }

    @Override
    public int getItemCount() {
        return tlist.size() + 2;
    }

    private class TitleHolder extends RecyclerView.ViewHolder {
        private TextView title;

        public TitleHolder(View itemView) {
            super(itemView);
            title = (TextView) itemView.findViewById(R.id.title);
        }
    }

    private class BlankHolder extends RecyclerView.ViewHolder {

        private View blank;

        public BlankHolder(View itemView) {
            super(itemView);
            blank = itemView.findViewById(R.id.blank);
        }
    }

    private class ItemHolder extends RecyclerView.ViewHolder {

        private ImageView img;
        private TextView tag_name;
        private TextView focus;

        public ItemHolder(View itemView) {
            super(itemView);
            img = (ImageView) itemView.findViewById(R.id.tag_pic);
            tag_name = (TextView) itemView.findViewById(R.id.tag_name);
            focus = (TextView) itemView.findViewById(R.id.focus);
        }
    }

    private class Cilck implements View.OnClickListener {

        private int position;

        public Cilck(int position) {
            this.position = position;
        }

        @Override
        public void onClick(View v) {
            final FollowTag tagDetail = tlist.get(position);
            switch (v.getId()) {
                case R.id.tag_name:
                case R.id.tag_pic:
                    Intent intent = new Intent();
                    intent.putExtra(gotoUtil.INTENT_VALUE_ONE, tagDetail.getTag_name());
                    intent.setClass(mContext, CircleTagSmoothActivity.class);
                    mContext.startActivity(intent);
                    break;
            }
        }
    }
}
